package protobuf;

import com.google.protobuf.InvalidProtocolBufferException;

import java.util.Arrays;
import java.util.List;

public class ProtoTest {

    public static void main(String[] args) {
        AddressBookProtos.Person.Builder personBuilder = AddressBookProtos.Person.newBuilder();
        personBuilder.setId(12345678);
        personBuilder.setName("Admin");
        personBuilder.addPhones(AddressBookProtos.Person.PhoneNumber.newBuilder().setNumber("111110").setType(AddressBookProtos.Person.PhoneType.MOBILE));
        personBuilder.addPhones(AddressBookProtos.Person.PhoneNumber.newBuilder().setNumber("222220").setType(AddressBookProtos.Person.PhoneType.HOME));
        personBuilder.addPhones(AddressBookProtos.Person.PhoneNumber.newBuilder().setNumber("333330").setType(AddressBookProtos.Person.PhoneType.WORK));

        AddressBookProtos.Person person = personBuilder.build();
        byte[] buff = person.toByteArray();

        try {
            AddressBookProtos.Person personOut = AddressBookProtos.Person.parseFrom(buff);
            System.out.printf("Id:%d, Name:%s\n", personOut.getId(), personOut.getName());

            List<AddressBookProtos.Person.PhoneNumber> phoneList = personOut.getPhonesList();

            for (AddressBookProtos.Person.PhoneNumber phone : phoneList) {
                System.out.printf("PhoneNumber:%s (%s)\n", phone.getNumber(), phone.getType());
            }

        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }

        System.out.println(Arrays.toString(buff));

    }

}
